package entityClasses;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;








import org.apache.commons.lang3.StringUtils;

import com.google.appengine.api.datastore.Entity;


public class Question implements Serializable{

     /**
	 * It's Serializable because we need to save it to session
	 */
	private static final long serialVersionUID = 1L;
	
		private String question_text = null;
        private String correct_answer = null;
        private List<String> incorrect_answers = new ArrayList<String>();
        private long points = 0;

        public Question() {
        }
       
        //Create question from entity.
        public Question(Entity qEnt) {
                this.setCorrectAnswer(qEnt.getProperty("correct_answer").toString());
                this.setQuestionText(qEnt.getProperty("question_text").toString());
                this.setPoints((long)qEnt.getProperty("points"));
                String incorrects = qEnt.getProperty("incorrect_answers").toString();
                this.incorrect_answers = Arrays.asList(incorrects.split("\\|"));
        }

        public String getQuestion_text() {
			return question_text;
		}

		public String getCorrect_answer() {
			return correct_answer;
		}

		public List<String> getIncorrect_answers() {
			return incorrect_answers;
		}

		public long getPoints() {
			return points;
		}

		public void setCorrectAnswer(String answer) {
                this.correct_answer = answer;
        }

        public void setQuestionText(String text) {
                this.question_text = text;
        }

        public void setPoints(long points) {
                this.points = points;
        }

        public void addIncorrectAnswer(String answer) {
                this.incorrect_answers.add(answer);
        }


       //Create entity from this question.
        public Entity toEntity() {
       
                Entity question = new Entity("question");
               
                question.setProperty("question_text", this.question_text);
                question.setProperty("points", this.points);
                question.setProperty("correct_answer", this.correct_answer);
                question.setProperty("incorrect_answers", StringUtils.join(this.incorrect_answers, "|"));
               
                return question;
        }

}

